package com.train.common.flow.listener;

import com.train.common.core.util.ApplicationContextHolder;
import com.train.common.flow.model.FlowTaskComment;
import com.train.common.flow.service.FlowTaskCommentService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;

/**
 * 将其他任务提交者赋值给当前任务审批者的监听器。
 *
 * @author xulei
 * @date 2025-03-07
 */
@Slf4j
public class FlowOtherTaskSubmitterListener implements TaskListener {

    private final transient TaskService taskService =
            ApplicationContextHolder.getBean(TaskService.class);
    private final transient FlowTaskCommentService flowTaskCommentService =
            ApplicationContextHolder.getBean(FlowTaskCommentService.class);

    @Override
    public void notify(DelegateTask t) {
        FlowTaskComment comment =
                flowTaskCommentService.getLatestFlowTaskComment(t.getProcessInstanceId(), t.getAssignee());
        if (comment != null) {
            taskService.setAssignee(t.getId(), comment.getCreateLoginName());
        }
    }
}
